package com.portal.admin.user.service;

import com.portal.admin.base.entity.BaseService;
import com.portal.admin.office.dao.SysOfficeDao;
import com.portal.admin.office.entity.SysOffice;
import com.portal.admin.role.dao.SysRoleDao;
import com.portal.admin.role.entity.SysRole;
import com.portal.admin.user.dao.SysUserDao;
import com.portal.admin.user.entity.SysUser;
import com.portal.admin.userRole.entity.SysUserRole;
import com.portal.admin.userRole.service.SysUserRoleService;
import com.portal.admin.utils.admin.IdGen;
import com.portal.admin.utils.admin.ObjectUtils;
import com.portal.admin.utils.admin.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

//用户信息接口类
@Service
public class SysUserService extends BaseService {

    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysOfficeDao sysOfficeDao;
    @Autowired
    private SysUserRoleService sysUserRoleService;

    //【用户信息】设置查询条件
    private LambdaQueryWrapper<SysUser> getSysUserQueryCondition(SysUser sysUser) {
        LambdaQueryWrapper<SysUser> lambdaQuery = this.getBaseQueryCondition(sysUser);
        //根据创建时间排序
        lambdaQuery.orderByDesc(SysUser::getUpdateDate);
        if (ObjectUtils.isNotNull(sysUser)) {
            //【登录名】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(sysUser.getLoginName()), SysUser::getLoginName, sysUser.getLoginName());
            //【密码】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(sysUser.getPassword()), SysUser::getPassword, sysUser.getPassword());
            //【姓名】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(sysUser.getUserName()), SysUser::getUserName, sysUser.getUserName());
            //【性别】精确查询
            lambdaQuery.eq(StringUtils.isNotNull(sysUser.getSex()), SysUser::getSex, sysUser.getSex());
            //【用户编号】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(sysUser.getUserNo()), SysUser::getUserNo, sysUser.getUserNo());
            //【身份证号】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(sysUser.getIdCard()), SysUser::getIdCard, sysUser.getIdCard());
            //【邮箱】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(sysUser.getEmail()), SysUser::getEmail, sysUser.getEmail());
            //【地址】模糊查询
            lambdaQuery.eq(StringUtils.isNotEmpty(sysUser.getAddress()), SysUser::getAddress, sysUser.getAddress());
            //【备注】模糊查询
            lambdaQuery.eq(StringUtils.isNotEmpty(sysUser.getRemarks()), SysUser::getRemarks, sysUser.getRemarks());
            //【电话】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(sysUser.getPhone()), SysUser::getPhone, sysUser.getPhone());
            //【电话】精确查询
            lambdaQuery.eq(StringUtils.isNotNull(sysUser.getDisabled()), SysUser::getDisabled, sysUser.getDisabled());
            //【机构】精确查询
            lambdaQuery.eq(StringUtils.isNotEmpty(sysUser.getOfficeId()), SysUser::getOfficeId, sysUser.getOfficeId());
        }
        return lambdaQuery;
    }


    //【用户信息】分页查询
    public IPage<SysUser> getPage(Page<SysUser> page, SysUser sysUser) {
        LambdaQueryWrapper<SysUser> lambdaQuery = getSysUserQueryCondition(sysUser);
        Page<SysUser> iPage = sysUserDao.selectPage(page, lambdaQuery);
        List<SysUser> records = iPage.getRecords();
        for (SysUser record : records) {
            String id = record.getId();
            List<SysRole> roleList = sysRoleDao.findRolesListByUserId(id);
            record.setRoleList(roleList);
            SysOffice office = sysOfficeDao.selectById(sysUser.getOfficeId());
            sysUser.setOffice(office);
        }
        //角色用户过滤
        if(StringUtils.isNotEmpty(sysUser.getRoleId())){//选择了查询角色
            List<SysUser> collect = records.stream().filter(item -> item.getRoleList().stream().map(SysRole::getId)
                    .collect(Collectors.toList())
                    .contains(sysUser.getRoleId())
            ).collect(Collectors.toList());
            iPage.setRecords(collect);
            iPage.setTotal(collect.size());
        }
        return iPage;
    }

    //【用户信息】查询列表
    public List<SysUser> getList(SysUser sysUser) {
        LambdaQueryWrapper<SysUser> lambdaQuery = getSysUserQueryCondition(sysUser);
        return sysUserDao.selectList(lambdaQuery);
    }

    //【用户信息】根据id查询
    public SysUser get(String id) {
        SysUser sysUser = sysUserDao.selectById(id);
        List<SysRole> roles = sysRoleDao.findRolesListByUserId(id);
        SysOffice office = sysOfficeDao.selectById(sysUser.getOfficeId());
        sysUser.setOffice(office);
        sysUser.setRoleList(roles);
        return sysUser;
    }

    //【用户信息】根据id查询
    public SysUser get(SysUser params) {
        LambdaQueryWrapper<SysUser> lambdaQuery = getSysUserQueryCondition(params);
        SysUser sysUser = sysUserDao.selectOne(lambdaQuery);
        return sysUser;
    }

    //【用户信息】新增
    public int insert(SysUser sysUser) {
        sysUser.preInsert();
        return sysUserDao.insert(sysUser);
    }

    //【用户信息】修改
    public int update(SysUser sysUser) {
        sysUser.preUpdate();
        return sysUserDao.updateById(sysUser);
    }

    //【用户信息】删除
    public int delete(String id) {
        //删除该用户下的关联的角色数据
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUserId(id);
        List<SysUserRole> list = sysUserRoleService.getList(sysUserRole);
        for (SysUserRole userRole : list) {
            sysUserRoleService.delete(userRole.getId());
        }
        return sysUserDao.deleteById(id);
    }

    //删除用户角色
    public int deleteRolesByUserId(String userId) {
        SysUserRole params = new SysUserRole();
        params.setUserId(userId);
        List<SysUserRole> list = sysUserRoleService.getList(params);
        for (SysUserRole sysUserRole : list) {
            sysUserRoleService.delete(sysUserRole.getId());
        }
        return list.size();
    }
    //更新用户角色
    public void updateUserRoles(SysUser sysUser) {
        SysUserRole entity = new SysUserRole();
        entity.setUserId(sysUser.getId());
        this.deleteRolesByUserId(sysUser.getId());
        List<SysRole> roleList = sysUser.getRoleList();
        if (roleList!=null && roleList.size() > 0) {
            for (SysRole role : roleList) {
                SysUserRole sysUserRole = new SysUserRole();
                sysUserRole.setId(IdGen.primaryKey());
                sysUserRole.setUserId(sysUser.getId());
                sysUserRole.setRoleId(role.getId());
                sysUserRoleService.insert(sysUserRole);
            }
        }
    }

    public SysUser findByLoginName(String loginName) {
        SysUser params = new SysUser();
        params.setLoginName(loginName);
        return this.get(params);
    }
}